﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    formSelects: 'formSelects-v4'
}).use(['form', 'layer', 'table', 'laypage', 'laydate', 'jquery'], function () {
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        player = parent.layer || layui.layer,
        laydate = layui.laydate;

    var examineDetailLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),//异步方法管理
                field: 'CreateTime',
                order: 'asc',
                data: undefined,
                projectAllList: "/Project/Info/LoadProjectAllList",
                listUrl: "/Staff/Examine/LoadDesignatedExamine",
                removeUrl: "/Staff/Examine/RemoveAttendance",
                saveUrl: "/Staff/Examine/ModifyAttendance",
            };

            this.Parameters.ac
                .pushQueue(function () {
                    self.logicFunc.loadProjectAllList(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    });
                })
                .pushQueue(function () {
                    self.initPage();
                    self.Parameters.ac.notifyQueueAsyncFuncComplate();
                })
                .pushQueue(this.bindEvent)
                .exec();

        },
        //初始化列表
        initPage: function () {

            laydate.render({
                elem: '#WorkTime',
                format: 'yyyy-MM-dd',
                value: new Date(),
                done: function (value, date, endDate) {

                }
            });

            //获取信息
            var data = GrdAssist.queryString("data");
            self.Parameters.data = data;
          
            self.logicFunc.loadListInfo(data);

            form.render();

        },
        //绑定事件
        bindEvent: function () {

            //关闭
            $("#btnClose").on("click", self.logicFunc.close);

            //修改
            $("#modifyButton").on("click", function () {
                self.logicFunc.modifyInfo();
            });
            //删除
            $("#removeButton").on("click", function () {
                self.logicFunc.removeInfo();
            });
        },
        /*
         * 逻辑方法
         */
        logicFunc: {

            //获取所有的工程信息
            loadProjectAllList: function (func) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.projectAllList,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            $("#ProjectNo").empty();
                            for (var i = 0; i < result.Data.length; i++) {
                                $("#ProjectNo").append("<option value=" + result.Data[i].ProjectNo + ">" + result.Data[i].ProjectName + "</option>");
                            }
                        }
                        if (func) {
                            func();
                        }
                    }
                });
            },

            //获取列表信息
            loadListInfo: function (data) {

                var jsonData = null;
                if (typeof data == "string") {
                    jsonData = JSON.parse(decodeURIComponent(data));
                } else {
                    jsonData = data;
                }
                var title =
                    [   { type: 'radio' },
                        {
                            field: 'WorkTime', title: '日期', width: 130, templet: function (d) {

                                return d.WorkTime.substring(0,10);
                            }
                        },
                        { field: 'WorkCount', width: 80, title: '天数' },
                        { field: 'CreateTime', title: '录入时间', width: 170, align: "center" },
                        { field: 'Creater', title: '录入人', width: 130, align: "center" },
                        { field: 'ProjectName', title: '项目名称', width: 120, align: "center" }
                    ];

                //table加载
                GrdLayUI.table({
                    radio: true,
                    dom: '#detailList',
                    height: "full-140",
                    url: self.Parameters.listUrl,
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    parameters: {
                        UserNo: jsonData.UserNo,
                        WorkTime: jsonData.WorkTime
                    },
                    title: title,
                    calback: function () {
                        $(".layui-table-view[lay-id='detailList'] .layui-table-body tr[data-index = '0' ] .layui-form-radio").click();

                    }
                });

                //监听行单击事件（双击事件为：rowDouble）
                table.on('row(detailList)', function (obj) {
                    var data = obj.data;
                    $("#ID").val(data.ID);
                    $("input[name='UserName']").val(data.UserName);
                    $("#ProjectNo").val(data.ProjectNo);
                    form.render('select');

                    $("#WorkTime").val(data.WorkTime.substring(0,10));
                    $("input[name='WorkCount']").val(data.WorkCount);
                    $("textarea[name='WorkContent']").val(data.WorkContent);

                });
            },




            //修改信息
            modifyInfo: function () {

                var json = {
                    parameters: GrdLayUI.initParamsData($("#detail-form"))
                };
                if (json.parameters.WorkTime=="") {
                    GrdLayUI.msg("请选择考情时间！", "警告");
                    return;
                }
                if (json.parameters.WorkCount == "") {
                    GrdLayUI.msg("请填写天数！", "警告");
                    return;
                }
                if ($("#ID").val()!="") {
                    GrdLayUI.ajaxPost({
                        url: self.Parameters.saveUrl,
                        data: json,
                        success: function (result) {
                            if (result.Code == 1000) {
                                GrdLayUI.msg(result.Msg, "成功");
                            } else {
                                GrdLayUI.msg(result.Msg, "警告");
                            }
                        }
                    });
                } else {
                    GrdLayUI.msg("请选中需要修改的考情信息！", "警告");
                }
            },
            //删除信息
            removeInfo: function () {
                GrdLayUI.removeDataById({
                    domId: "detailList",
                    removeUrl: self.Parameters.removeUrl,
                    backAction: function (data) {
                        self.logicFunc.loadListInfo(self.Parameters.data);
                    }
                });
            },
            //取消
            close: function () {
                player.closeAll();
                
                //刷新父页面
                //window.parent.location.reload();
            }
        },
        //工具
        tools: {
        }
    };
    examineDetailLogic.init();
});
